
package com.phevos.common.dbpool;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: from croot cts project
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author zhaoch
 * @version 1.0
 */

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * �û��Զ���DAO�ĸ���
 */
public abstract class GenericDAO extends BaseDAO
{
    /**
     * 
     */
    public static final boolean PSTMT_UNUSED = false;

    /**
     * ������
     * 
     * @param transMgr ���ڻ��һ�� DBConnection �Ķ���ʵ��@param conn - * ������
     * @param conn���ڻ��һ�� Connection �Ķ���ʵ��
     * @throws java.sql.SQLException
     */
    GenericDAO(Connection conn) throws SQLException
    {
        super(conn);
    }

    /**
     * ���캯��
     * 
     * @throws java.sql.SQLException
     */
    GenericDAO()
    {
        super();
    }

    /**
     * @param stmt
     * @param flag
     * @param obj
     * @throws SQLException
     * @throws java.sql.SQLException
     */
    protected abstract void fillFindStatement(PreparedStatement stmt, int flag, Object obj)
            throws SQLException;

    /**
     * @param rs
     * @param flag
     * @param obj
     * @throws SQLException
     * @throws java.sql.SQLException
     */
    protected abstract void extractFromFindResultSet(ResultSet rs, int flag, Object obj)
            throws SQLException;

    /**
     * This is the find method will use for "SELECT" sql operation tailor in EJB and non-EJB
     * enviorment.
     * 
     * @param queryString The query string.
     * @param preparedStmt The parameter help to judge if use PreparedStatement.Set to "true" to
     *            use, "false" to not use.
     * @param flag This parameter will determind which query statement to be execute. The value come
     *            from yourself DAO. If only one query statement set flag equal zero.
     * @param obj The object you want to fill or extract value to it.
     * @return The result set count .If the operation can't find anything, reurn zero.
     * @throws java.sql.SQLException
     */
    protected int doFind(String queryString, boolean preparedStmt, int flag, Object obj)
            throws SQLException
    {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int i = 0;
        try
        {
            getConnection();
            stmt = conn.prepareStatement(queryString);
            if (preparedStmt)
            {
                fillFindStatement(stmt, flag, obj);
            }
            rs = stmt.executeQuery();
            if (rs.next())
            {
                i++;
                extractFromFindResultSet(rs, flag, obj);
            }
            return i;

        }
        catch (SQLException sqe)
        {
            logger.debug(sqe.getMessage());
            throw sqe;
        }
        finally
        {
            closeResultset(rs);
            closeStatement(stmt);
            closeConnection();
        }
    }

    /**
     * This method will judge if the object existed in DataBase.
     * 
     * @param queryString
     * @return boolean
     * @throws SQLException
     * @throws java.sql.SQLException
     */
   /* private boolean isExisted(String queryString) throws SQLException
    {
        Statement stmt = null;
        ResultSet result = null;
        boolean returnValue = false;

        try
        {
            getConnection();
            stmt = conn.createStatement();
            result = stmt.executeQuery(queryString);

            if (!result.next())
            {
                returnValue = false;
            }
            else
            {
                if (result.getInt(1) == 0)
                    returnValue = false;
                else
                    returnValue = true;
            }

            return returnValue;
        }
        catch (SQLException e)
        {
            logger.debug("isExisted = " + e);
            throw e;
        }
        finally
        {
            closeResultset(result);
            closeStatement(stmt);
            closeConnection();
        }
    }*/

}
